package dao

import (
	"alan-snippet/internal/models"
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func InitMysql() {

	dsn := "root:vader20011014@(localhost:3306)/alan-snippet?charset=utf8mb4&parseTime=True&loc=Local"
	var err error
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal(err)
		return
	}

	// 自动迁移
	// GORM 会根据结构体定义自动创建或更新表结构
	err = DB.AutoMigrate(&models.Snippet{}, &models.Tag{}, &models.User{})
	if err != nil {
		log.Fatalf("failed to auto migrate: %v", err)
	}
	log.Println("Database connection and migration successful!")
}
